package cn.doitedu.day02

import org.apache.spark.rdd.RDD
import org.apache.spark.{SparkConf, SparkContext}

object T08_FlatMapValuesDemo {

  def main(args: Array[String]): Unit = {

    //1.创建SparkConf
    val conf = new SparkConf().setAppName("MapPartitionsWithIndexDemo")
      .setMaster("local[4]")

    //2.创建SparkContext
    val sc = new SparkContext(conf)

    val lst = List(("spark", "1,2,3"), ("hive", "4,5"), ("hbase", "6"), ("flink", "7,8"))
    val rdd1: RDD[(String, String)] = sc.parallelize(lst, 2)

    //将value打平，再将打平后的每一个元素与key组合("spark", "1,2,3") =>（"spark",1）,（"spark",2),（"spark",3）
//    val rdd2: RDD[(String, Int)] = rdd1.flatMap{
//      case(k, v) => {
//        v.split(",").map(e => {
//          (k, e.toInt)
//        })
//      }
//    }

    val rdd2: RDD[(String, Int)] = rdd1.flatMapValues(_.split(",").map(_.toInt))

    rdd2.foreach(println)

  }

}